﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;
using Box.BL;
using Box.Entities;
using Microsoft.Practices.Unity;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.IO;
namespace Box.Cliente.Logistica.Nota_Pedidos
{
    public partial class frmListNotaPedido : System.Web.UI.Page
    {
        [Dependency]
        public INotaPedidoBL notaPedidoBl { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            var accion = Request.QueryString["accion"];
            var id = Request.QueryString["id"];
            
            if (accion == "pdf")
            {
                var identificador = int.Parse(id);
                var nota = notaPedidoBl.GetFromNotaPEdidoById(identificador);

                Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

                try
                {
                    PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);

                    //Open PDF Document to write data 
                    pdfDoc.Open();


                    string cadenaFinal = "";
                    string path = Server.MapPath("/Imagenes/LOGOTIPO_BOXSPORT.png");
                    string nomnota = nota.CodigoNotaPedido.ToString();
                    cadenaFinal += "<img src='" + path + "' Height='320' Width='350' /><br/><br/>";
                    cadenaFinal += "<TABLE BORDER='1'><TR><TD>NOMBRE :</TD><TD>" + nomnota + "</TD></TR>" +
                                    "<TR><TD>APELLIDO :</TD><TD>MELGAREJO LIMAS</TD></TR>" +
                                    "<TR><TD>EDAD :</TD><TD>24</TD></TR></TABLE>";
                    //Assign Html content in a string to write in PDF 
                    string strContent = cadenaFinal;

                    //Read string contents using stream reader and convert html to parsed conent 
                    var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(strContent), null);

                    //Get each array values from parsed elements and add to the PDF document 
                    foreach (var htmlElement in parsedHtmlElements)
                        pdfDoc.Add(htmlElement as IElement);

                    //Close your PDF 
                    pdfDoc.Close();

                    Response.ContentType = "application/pdf";

                    //Set default file Name as current datetime 
                    Response.AddHeader("content-disposition", "attachment; filename=DEMO.pdf");
                    System.Web.HttpContext.Current.Response.Write(pdfDoc);

                    Response.Flush();
                    Response.End();

                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }


            }
            var opc = Request.QueryString["opc"];
            var idc = Request.QueryString["id"];
            if (opc != null && opc == "delete" && idc != null)
            {
                var idcc = Int32.Parse(idc.ToString());
                notaPedidoBl.RemoveNotaPedido(idcc);
                Response.Redirect("frmListNotaPedido.aspx");
            }
        }

        public IEnumerable<NotaPedido> GetNotas([Control("txtCriterio")]string criterio)
        {

            return notaPedidoBl.GetFromNotaPedidoByCriterio(criterio);
        }
    }
}